/**
 * Created by hetanhong on 2014/9/10.
 */
(function () {
    var dcdb = function (config) {
        this._config = config;
        this._type = this.getConfig("type");
        this._code = this.getConfig("code");
        this._id = this.getConfig("id");
        this._taskName = this.getConfig("taskName");
        this.width = window.innerWidth || 320;
        this.height = window.innerHeight || 480;
    };
    if (typeof (Clickmed) === "undefined") {
        Clickmed = {};
    }
    Clickmed.dcdb = dcdb;
    dcdb.prototype.init = function () {
        var me = this;
        console.log(me);
        var url = serverURL + "cmcp/phone/getOtherDetailInfo";
        window.plugins.getIntent.show(["OA", "正在获取数据."]);
        var self = {
            type: me._type,
            id: me._id,
            code: me._code
        };
        PostCP(url, self, function (dat) {
            window.plugins.getIntent.hide();
            console.log(dat);
            me.dat = dat;
            me.draw();
        }, function () {
            window.plugins.getIntent.hide();
            Alert("获取数据失败");
        });
    };
    dcdb.prototype.draw = function () {
        var me = this;
        var render = this._config.render || d3.select("body");
        var d1 = this._config.body || d3.select("body");
        var width = this.width;
        var scrol = d1.append("div").classed("scrol", true).style({
            width: "100%",
            height: "100%",
            position: "absolute",
            left: this.width + "px",
            top: "0px"
        }).attr("id", "scrol");
        d3.select(render).transition().duration(Transiacion).style("left", 0 - width + "px").style("position", "absolute").style({
            display: "none"
        });

        scrol.transition().duration(Transiacion).style("left", "0px");
        var warp = scrol.append("div").classed("dt_warp", true).style({
            position: "relative"
        });
        var datong_header = warp.append("div").classed("datong_header", true);

        var go_back = datong_header.append("div").classed("go_back", true);
        go_back.html("<i><img src =../../common/page/images/go_back_ico.png width =20 height=auto></i>返回");
        go_back.on(touch_s, function () {
            $(this).css("background-color", "#005ebf");
        });
        go_back.on(touch_e, function () {
            $(this).css("background-color", "");
            d1.selectAll(".scrol").transition().duration(Transiacion).style("left", width + "px");
            d3.select(render).transition().duration(Transiacion1).style("left", 0 + "px").style({
                display: "block"
            });

            setTimeout(function () {
                d1.selectAll(".scrol").remove();
                Android.back = TempFunction.back;
            }, Transiacion);

        });
        function back1() {
            d1.selectAll(".scrol").transition().duration(Transiacion).style("left", width + "px");
            d3.select(render).transition().duration(Transiacion1).style("left", 0 + "px").style({
                display: "block"
            });

            setTimeout(function () {
                d1.selectAll(".scrol").remove();
            }, Transiacion);
            Android.back = TempFunction.back;
        }

        // Android返回键动作
        Android.back = TempFunction.back1 = back1;
        datong_header.append("h1").html(me._taskName || "");
        me.showAll();
    };
    dcdb.prototype.showAll = function () {
        var me = this;
        var me = this;
        var content = me.dat;
        var warp = d3.select("#scrol .dt_warp");

        var scrol_ = warp.append("div").classed("scrol_", true).style({
            width: "100%",
            height: height - 45 + "px",
            postion: "relative",
            overflow: "hidden"
        }).attr("id", "scrol_");
        var datong_con = scrol_.append("div").classed("datong_con datong_con_re", true);

        var titleInfo = content["titleInfo"];
        for (var title in titleInfo) {
            datong_con.append("div").classed("title", true).style({
                "text-align": "center"
            }).html(titleInfo[title]).classed("tongzhiTitle", true);
        }
        var datong_con = datong_con.append("div").classed("phone_cotent", true);
        var baseInfo = content["baseInfo"];
        var ul = datong_con.append("ul");
        var li = ul.append("li").classed("b_btm", true);
        var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0");
        for (var base in baseInfo) {
            var tr = table.append("tr");
            tr.append("td").html("<div>" + base.split("").join("&nbsp") + "</div>").style({
                width: width * 0.25 + "px"
            });
            tr.append("td").attr({
                width: width * 0.75 + "px"
            }).append("div").html(baseInfo[base]).classed("extend", true);

        }

        var superviseInfo = content["superviseInfo"];
        if (superviseInfo && superviseInfo.length) {
            datong_con.append("div").classed("title", true).style({
                "text-align": "center"
            }).html('督办信息').classed("report_title", true);
            var ul = datong_con.append("ul");
            var li = ul.append("li").classed("b_btm", true);
            for (var i = 0; i < superviseInfo.length; i++) {
                var table = li.append("table").attr("cellpadding", "0").attr('cellspacing', "0");
                for (var supervise in superviseInfo[i]) {
                    var tr = table.append("tr");
                    tr.append("td").html("<div>" + supervise.split("").join("&nbsp") + "</div>").style({
                        width: width * 0.25 + "px"
                    });
                    tr.append("td").attr({
                        width: width * 0.75 + "px"
                    }).append("div").html(superviseInfo[i][supervise]).classed("extend", true);
                }
            }
        }
        newScroll = new iScroll("scrol_", {hScrollbar: false, vScrollbar: false});
    };
    dcdb.prototype.getConfig = function (key) {
        if (this._config == null) {
            return null;
        } else {
            if (typeof (this._config[key]) != "undefined") {
                return this._config[key];
            } else {
                return null;
            }
        }
    };
})();